package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.common.PageRecv;
import com.gxa.common.R;
import com.gxa.mapper.CustomerEmployeeMapper;
import com.gxa.mapper.CustomerMapper;
import com.gxa.mapper.RecycleMapper;
import com.gxa.pojo.Customer;
import com.gxa.pojo.Customer_Employee;
import com.gxa.pojo.Employee;
import com.gxa.pojo.Recycle;
import com.gxa.service.RecycleService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class RecycleServiceImpl implements RecycleService {
    @Autowired
    private RecycleMapper recycleMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomerEmployeeMapper customerEmployeeMapper;
    @Override
    @Transactional(rollbackFor =Exception.class)
    public R addRecycle(Recycle recycle) {
        R r=new R();
        recycleMapper.addRecycle(recycle);
        r.setCode(200);
        r.setMsg("添加成功");
        return r;
    }

    @Override
    @Transactional(rollbackFor =Exception.class)
    public JsonResult selectRecycle(PageRecv pageRecv) {
        Integer param1=(pageRecv.getCurrentPage()-1)* pageRecv.getPageSize();
        pageRecv.setCurrentPage(param1);
        List<Recycle> recycleList=recycleMapper.selectRecycle(pageRecv);
        Integer count= recycleMapper.selectCountRecycle(pageRecv);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setData(recycleList);
        jsonResult.setCount(count);
        return jsonResult;
    }
    @Override
    @Transactional(rollbackFor =Exception.class)
    public R deleteRecycle(List<Integer> recycleids) {
        R r=new R();
        recycleMapper.deleteRecycle(recycleids);
        r.setCode(200);
        r.setMsg("删除成功");
        return r;
    }

    @Override
    @Transactional(rollbackFor =Exception.class)
    public R deleteAllRecycle() {
        R r=new R();
        recycleMapper.deleteAllRecycle();
        r.setCode(200);
        r.setMsg("清空回收站成功");
        return null;
    }

    @Override
    @Transactional(rollbackFor =Exception.class)
    public JsonResult searchRecycle(PageRecv pageRecv) {
        Integer param1=(pageRecv.getCurrentPage()-1)* pageRecv.getPageSize();
        pageRecv.setCurrentPage(param1);
        List<Recycle> recycleList= recycleMapper.searchRecycle(pageRecv);
        Integer count=recycleMapper.searchCountRecycle(pageRecv);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode(200);
        jsonResult.setMsg("搜索成功");
        jsonResult.setData(recycleList);
        jsonResult.setCount(count);
        return jsonResult;
    }

    //还原
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R reduction(List<Integer> recycleids) {
        //还原该状态且还原原业务员
        //状态码改为1，在中间表添加员工和客户的对应关系
        R r=new R();
        Customer customer=new Customer();
        Employee employee=new Employee();
        Customer_Employee customer_employee=new Customer_Employee();
        List<Recycle> recycleList=recycleMapper.reductionAndTransfer(recycleids);
        for (int i=0;i<recycleList.size();i++){
//            拿客户id修改客户状态
            //把客户id和员工id封装成对象新增到中间表
            customer.setCustomer_id(recycleList.get(i).getCustomer_id());
            employee.setEmployee_id(recycleList.get(i).getSalesman_id());
            customer.setStatus(1);
            customerMapper.updateCustomer(customer);
            customer_employee.setCustomer(customer);
            customer_employee.setEmployee(employee);
            customerEmployeeMapper.insertRelationship(customer_employee);
            //最后再调一次删除，删除掉这些客户的回收站记录
        }
        //还原后删除回收站记录
        recycleMapper.deleteRecycle(recycleids);
        r.setCode(200);
        r.setMsg("成功");
        return r;
    }

    //移入公海
    @Override
    public R transfer(List<Integer> recycleids) {
        //移入公海只改字段
        //状态码改为2
        R r=new R();
        Customer customer=new Customer();
        List<Recycle> recycleList=recycleMapper.reductionAndTransfer(recycleids);
        for (int i=0;i<recycleList.size();i++){
//            拿客户id修改客户状态
            //把客户id和员工id封装成对象新增到中间表
            customer.setCustomer_id(recycleList.get(i).getCustomer_id());
            customer.setStatus(2);
            customerMapper.updateCustomer(customer);
            //最后再调一次删除，删除掉这些客户的回收站记录
        }
        //还原后删除回收站记录
        recycleMapper.deleteRecycle(recycleids);
        r.setCode(200);
        r.setMsg("成功");
        return r;
    }
}
